<?php
class Front_Product_Model extends BaseModel{
	public function getForViewDetails($id){
		if(!isset($id) || $id == "")
			return null;
		$sqlUpdate = "UPDATE product set hit = hit + 1 where id='".$id."'";
		$row = DBHelper::getInstance()->execute($sqlUpdate);
		$sql = "SELECT p.id, p.name, p.description, p.details, p.price1, p.image, p.remark,
				p.productcat, p.producttype, p.manufacture,p.warranty,
				c.name as ".Product::CATNAME.",c.id as catid,
				t.name as ".Product::TYPENAME.",
				m.name as ".Product::MANUFACTURENAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				LEFT JOIN producttype as t ON t.id = p.producttype
				LEFT JOIN productmanufacture as m ON m.id = p.manufacture
				WHERE p.id = '".$id."'";
		return DBHelper::getInstance()->fetchRow($sql);	
	}
	public function getDetailsForURL($id){
		$sql = "SELECT p.id,p.name,c.name as ".Product::CATNAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				WHERE p.id='".$id."'";
		return DBHelper::getInstance()->fetchRow($sql);
	}
	public function getRelateCatById($context, $id){
		$sql = "SELECT p.id, p.name,p.image,p.price1,c.name as ".Product::CATNAME." 
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				WHERE p.productcat IN 
				( SELECT d.productcat from product d WHERE d.id = '".$id."')
						AND p.id != '".$id."'
				ORDER BY p.".Product::CREATETIME." DESC
				 limit 0,6";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::productDetails1($context, $value['id'], $value['name'], $value[Product::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getRelateTypeById($context, $id){
		$sql = "SELECT p.id, p.name,p.image,p.price1,c.name as ".Product::CATNAME.",t.name as ".Product::TYPENAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				LEFT JOIN producttype as t ON t.id = p.producttype
				WHERE p.producttype IN
				( SELECT d.producttype from product d WHERE d.id = '".$id."')
						AND p.id != '".$id."'
				ORDER BY p.".Product::CREATETIME." DESC
				 limit 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::productDetails1($context, $value['id'], $value['name'], $value[Product::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getAllForCatView($context, $catid){
		$sql = "SELECT p.id, p.name, p.image,p.price1, c.name as ".Product::CATNAME."
				FROM product p
				LEFT JOIN productcat as c ON c.id = p.productcat
				WHERE p.productcat = '".$catid."'
				ORDER BY p.ordinal ASC ";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::productDetails1($context, $value['id'], $value['name'], $value[Product::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getProductCatName($catid){
		$sql = "SELECT name from productcat where id='".$catid."'";
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(count($result)>0)
			return $result['name'];
		else return "";
	}
}